<?php
defined('SYS_URL_FIX') or define('SYS_URL_FIX', '.html');
defined('SYS_URL_FIX_STILL') or define('SYS_URL_FIX_STILL', 'CMSSTILL');
defined('SYS_URL_FIX_PORT') or define('SYS_URL_FIX_PORT', 'CMSPORT');
class ManagePages {
    public $limit;                   //limit字串
    public $limitArr;                //limit起止数组
    public $dataTotal;               //总记录数
    public $pageSize;                //每页几条
    public $nowPage;                 //当前第几页
    public $pageNum;                 //总页数
    private $bothNum;                //分页数字当前页两边有几个可点页
    private $flag;                   //url中分隔符
    private $url;                    //当前url

    //构造方法
    public function __construct($nowPage = 1, $dataTotal, $pageSize = 10, $flag='/'){
        $this->bothNum = 2;
        $this->flag = $flag;
        $this->dataTotal = intval($dataTotal) ? intval($dataTotal) : 1;
        $this->pageSize = intval($pageSize) ? intval($pageSize) : 1;
        $this->pageNum = ceil($this->dataTotal / $this->pageSize);
        $this->nowPage = $this->getNowPage($nowPage);
        $this->limit = $this->getLimit();
        $this->limitArr = $this->limitArr();
        $this->url = $this->getUrl();
    }

    /**
     * Notes: 分页按钮
     * User: ZhuHaili
     * Date: 2020/11/5
     * @param string $lang
     * @return array
     */
    public function show($lang=''){
        return array(
            'f' => $this->first($lang),
            'p' => $this->prev($lang),
            's' => $this->pageList($lang),
            'n' => $this->next($lang),
            'l' => $this->last($lang),
            //'i' => $this->pageInfo($lang)
            //'j' => $this->pageJump($lang),
        );
    }

    /**
     * Notes: 获取当前页是第几页
     * User: ZhuHaili
     * Date: 2020/10/17
     * @return float|int|mixed
     */
    private function getNowPage($nowPage){
        $nowPage = intval($nowPage) > 0 ? intval($nowPage) : 1;
        if ($nowPage > 1) {
            if ($nowPage > $this->pageNum) {
                $nowPage = $this->pageNum;
            }
        }
        return $nowPage;
    }

    /**
     * Notes: 解析url
     * User: ZhuHaili
     * Date: 2020/11/5
     * @return string
     */
    private function getUrl(){
        $http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
        $url = $_SERVER['REQUEST_URI'] ? $_SERVER['REQUEST_URI'] : '';
        if(strpos($url,'/'.SYS_URL_FIX_STILL.'/1') !== false){
            $urlDelStr = strpos($url,"/".SYS_URL_FIX_STILL."/1");
            $url = substr_replace($url,"",$urlDelStr, strlen(SYS_URL_FIX_STILL)+3);
        }
        if(strpos($url,'/'.SYS_URL_FIX_PORT.'/2') !== false){
            $urlDelStr = strpos($url,"/".SYS_URL_FIX_PORT."/2");
            $url = substr_replace($url,"",$urlDelStr, strlen(SYS_URL_FIX_PORT)+3);
        }
        $flag = $this->flag;
        $urlArr = explode($flag, $url);
        $urlArrNo = array();
        foreach($urlArr as $k=>$v){
            if($v) {
                if(SYS_URL_FIX){
                    if(strpos(strtolower($v),SYS_URL_FIX) !== false){
                        $len = strlen(SYS_URL_FIX);
                        $v = substr($v, 0, -$len);
                    }
                }
                $urlArrNo[$k] = $v;
            }
        }
        $urlArrKey = array_flip($urlArrNo);
        $pageKey = count($urlArrNo);
        if(isset($urlArrKey['page'])){
            $pageKey = $urlArrKey['page'];
        }else{
            $pageKey = $pageKey+1;
            $urlArrNo[$pageKey] = 'page';
        }
        $pageKey = $pageKey+1;
        $urlArrNo[$pageKey] = '{page}';
        $newUrl = implode($flag, $urlArrNo);
        $newUrl = $http_type.$_SERVER['HTTP_HOST'].'/'.$newUrl.SYS_URL_FIX;
        return $newUrl;
    }

    /**
     * Notes: 计算limit起止sql字符串
     * User: ZhuHaili
     * Date: 2020/11/5
     * @return string
     */
    private function getLimit(){
        if ($this->nowPage == $this->pageNum) {
            return 'LIMIT '.($this->nowPage - 1) * $this->pageSize . ', '.($this->dataTotal - ($this->nowPage - 1) * $this->pageSize);
        } else {
            return 'LIMIT '.($this->nowPage - 1) * $this->pageSize . ', '. $this->pageSize;
        }
    }

    /**
     * Notes: 计算limit起止数组
     * User: ZhuHaili
     * Date: 2020/11/5
     * @return array
     */
    private function limitArr(){
        if ($this->nowPage == $this->pageNum) {
            $start = ($this->nowPage - 1) * $this->pageSize;
            $end = ($this->dataTotal - ($this->nowPage - 1) * $this->pageSize);
            return array($start, $end);
        } else {
            $start = ($this->nowPage - 1) * $this->pageSize;
            $end = $this->pageSize;
            return array($start, $end);
        }
    }

    /**
     * Notes: 分页数字按钮
     * User: ZhuHaili
     * Date: 2020/11/5
     * @return string
     */
    private function pageList(){
        $url = $this->getUrl();
        $pageList = '';
        /*if (($this->nowPage - $this->bothNum) > 1) {
            $pageList .= ' ... ';
        }*/
        // left pageNo
        for($i = $this->bothNum; $i > 0; $i--) {
            $_page = $this->nowPage - $i;
            if ($_page < 1 ){
                continue;
            }
            $newUrl = str_replace("{page}",$_page,$url);
            $pageList .= ' <a href="'.$newUrl.'">'.$_page.'</a> ';
        }
        // now page
        if($this->pageNum > 1){
            $pageList .= ' <span class="act">' . $this->nowPage . '</span> ';
        }
        //right pageNo
        for ($i = 1; $i <= $this->bothNum; $i++){
            $_page = $this->nowPage + $i;
            if ($_page > $this->pageNum){
                continue;
            }
            $newUrl = str_replace("{page}",$_page,$url);
            $pageList .=' <a href="'.$newUrl.'">'.$_page.'</a> ';
        }

        if ($this->nowPage + $this->bothNum < $this->pageNum) {
            if(($_page+1) < $this->pageNum){
                $pageList .= ' ... ';
            }
        }
        if($_page < $this->pageNum){
            $url = $this->getUrl();
            $newUrl = str_replace("{page}",$this->pageNum,$url);
            $pageList .='<a href="'.$newUrl.'">'.$this->pageNum.'</a>';
        }


        return $pageList;

    }

    /**
     * Notes: 首页
     * User: ZhuHaili
     * Date: 2020/11/5
     * @param $lang
     * @return string
     */
    private function first($lang){
        if ($this->nowPage > 1) {
            $url = $this->getUrl();
            $newUrl = str_replace("{page}","1",$url);
            if(strtolower($lang) == 'en'){
                return '<a href="'.$newUrl.'">First</a>';
            }
            return '<a href="'.$newUrl.'">首页</a>';
        }
    }

    /**
     * Notes: 尾页
     * User: ZhuHaili
     * Date: 2020/11/5
     * @param $lang
     * @return string
     */
    private function last($lang){
        if ($this->nowPage < $this->pageNum) {
            $url = $this->getUrl();
            $newUrl = str_replace("{page}",$this->pageNum,$url);
            if(strtolower($lang) == 'en'){
                return '<a href="'.$newUrl.'">Last</a>';
            }
            return '<a href="'.$newUrl.'">尾页</a>';
        }
    }

    /**
     * Notes: 上一页
     * User: ZhuHaili
     * Date: 2020/11/5
     * @param $lang
     * @return string
     */
    private function prev($lang){
        if($this->nowPage > 1) {
            $url = $this->getUrl();
            $newUrl = str_replace("{page}",($this->nowPage - 1),$url);
            if(strtolower($lang) == 'en'){
                return '<a href="'.$newUrl.'">Prev</a>';
            }
            return ' <a href="'.$newUrl.'" class="prev"> 上一页 </a> ';
        }
    }

    /**
     * Notes: 下一页
     * User: ZhuHaili
     * Date: 2020/11/5
     * @param $lang
     * @return string
     */
    private function next($lang){
        if($this->nowPage < $this->pageNum) {
            $url = $this->getUrl();
            $newUrl = str_replace("{page}",($this->nowPage + 1),$url);
            if(strtolower($lang) == 'en'){
                return '<a href="'.$newUrl.'">Next</a>';
            }
            return ' <a href="'.$newUrl.'" class="next"> 下一页 </a> ';
        }
    }

    /**
     * Notes: 分页当前/总页数信息
     * User: ZhuHaili
     * Date: 2020/11/5
     * @param $lang
     * @return string
     */
    private function pageInfo($lang){
        if(strtolower($lang) == 'en'){
            return '<span class="pageInfos"><span class="pageInfo_now">'.$this->nowPage.'</span> / <span class="pageInfo_last">'.$this->pageNum.'</span></span>';
        }
        return '<span class="pageInfos"><span class="pageInfo_now">'.$this->nowPage.'</span> / <span class="pageInfo_last">'.$this->pageNum.'</span></span>';
    }

    /**
     * Notes: 解析url2
     * User: ZhuHaili
     * Date: 2019/11/5
     * @return string
     */
    private function getUrl2(){
        $http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
        $url = $_SERVER['REQUEST_URI'] ? $_SERVER['REQUEST_URI'] : '';
        if(strpos($url,'/'.SYS_URL_FIX_STILL.'/1') !== false){
            $urlDelStr = strpos($url,"/".SYS_URL_FIX_STILL."/1");
            $url = substr_replace($url,"",$urlDelStr,strlen(SYS_URL_FIX_STILL)+3);
        }
        if(strpos($url,'/'.SYS_URL_FIX_PORT.'/2') !== false){
            $urlDelStr = strpos($url,"/".SYS_URL_FIX_PORT."/2");
            $url = substr_replace($url,"",$urlDelStr, strlen(SYS_URL_FIX_PORT)+3);
        }
        $flag = $this->flag;
        $urlArr = explode($flag, $url);
        $urlArrNo = array();
        foreach($urlArr as $k=>$v){
            if($v) {
                if(SYS_URL_FIX){
                    if(strpos(strtolower($v),SYS_URL_FIX) !== false){
                        $len = strlen(SYS_URL_FIX);
                        $v = substr($v, 0, -$len);
                    }
                }
                $urlArrNo[$k] = $v;
            }
        }
        $urlArrKey = array_flip($urlArrNo);
        $pageKey = count($urlArrNo);
        if(isset($urlArrKey['page'])){
            $pageKey = $urlArrKey['page'];
        }else{
            $pageKey = $pageKey+1;
            $urlArrNo[$pageKey] = 'page';
        }
        if(end($urlArrNo) != 'page'){
            array_pop($urlArrNo);
        }
        $newUrl = implode($flag, $urlArrNo);
        $newUrl = $http_type.$_SERVER['HTTP_HOST'].'/'.$newUrl;
        return $newUrl;
    }

    /**
     * Notes:跳页
     * User: ZhuHaili
     * Date: 2023/5/25
     */
    private function pageJump($lang){
        $button = '跳转';
        if(strtolower($lang) == 'en'){
            $button = 'Jump';
        }
        $url = $this->getUrl2();
        $t = '<label class="page_jump"><input type="number" value="'.$this->nowPage.'" class="page_jump_inp"> <button class="page_jump_btn" >'.$button.'</button> ';
        $t .= '<script>';
        $t .= '
                var pageclassIndex = \'\'; // 当前操作的class是第几个(设置全局变量其他地方也可以用)
                var pageJumpDom = document.getElementsByClassName(\'page_jump_btn\'); // 假设class有多个
                var pageMaxNo = "'.$this->pageNum.'";
                // 循环
                for(var i=0;i<pageJumpDom.length;i++){
                  pageJumpDom[i].setAttribute("index",i);
                  pageJumpDom[i].onclick = function(){
                    pageclassIndex = this.getAttribute("index"); // 当前操作的class下标
                    pageJNo = parseInt(pageJNo);
                    pageJNo = pageJNo > 0 ? pageJNo : 1;
                    pageJNo = pageJNo >= pageMaxNo ? pageMaxNo : pageJNo;
                    console.log("'.$url.'/"+pageJNo+"'.SYS_URL_FIX.'");
                    setTimeout(function(){window.location.href="'.$url.'/"+pageJNo+"'.SYS_URL_FIX.'";},1000)
                  }
                }
                ';
        $t .= '</script>';
        return $t;
    }

}

/////////////////////////////////////////////////////////////////
/*
$url = $_SERVER['REQUEST_URI'] ? $_SERVER['REQUEST_URI'] : '';
$urlArr = explode('/', $url);
$nowPage = 1;
foreach($urlArr as $k=>$v){
    if( $v == 'page'){
        $nowPage = $urlArr[$k+1];
    }
}

$pageSize = 3;
$dataTotal = 0;
$con = mysqli_connect('127.0.0.1','root','123456','lc_luchang');
$sql = "select count(*) dataTotal from lc_article";
$res = mysqli_query($con,$sql);
while($row = mysqli_fetch_assoc($res)) {
    $dataTotal = $row['dataTotal'];
}
if($dataTotal <= 0){
    exit('errer:No size');
}

$page = new ManagePages($nowPage, $dataTotal, $pageSize);

$sql = "select * from lc_article ".$page->limit;
$res = mysqli_query($con,$sql);
while($row = mysqli_fetch_assoc($res)) {
    echo '-----'.$row['id'].'-----'.$row['title'].'-----<br>';
}

$page->show();*/
